﻿using System;
using System.Linq;
using System.Windows;
using GalaSoft.MvvmLight;
using SverigesRadio.SverigesRadioApp.Model;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels
{
    public class ChannelInformationViewModel : ViewModelBase
    {
        public ChannelInformationViewModel(Channel channel)
        {
            Id = channel.Id;
            Channel = channel;
        }

        public string Id { get; set; }
        public Channel Channel { get; private set; }

        private string currentProgramTitle;
        public string CurrentProgramTitle
        {
            get { return currentProgramTitle; }
            set
            {
                currentProgramTitle = value;
                RaisePropertyChanged("CurrentProgramTitle");
            }
        }

        private string currentProgramInfo;
        public string CurrentProgramInfo
        {
            get { return currentProgramInfo; }
            set
            {
                currentProgramInfo = value;
                RaisePropertyChanged("CurrentProgramInfo");
            }
        }

        public Visibility CurrentProgramInfoVisibility
        {
            get
            {
                if (string.IsNullOrEmpty(CurrentProgramInfo))
                    return Visibility.Collapsed;
                return Visibility.Visible;
            }
        }

        private string nextProgramTitle;
        private string nextProgramStartTime;
        private string nextProgramStopTime;
        private string programUrl;
        private string nextProgramUrl;

        public string NextProgramTitle
        {
            get { return nextProgramTitle; }
            set
            {
                nextProgramTitle = value;

                RaisePropertyChanged("NextProgramTitle");
            }
        }

        public string NextProgramStartTime
        {
            get
            {
                return nextProgramStartTime;
            }
            set
            {
                nextProgramStartTime = value;
                RaisePropertyChanged("NextProgramStartTime");
            }
        }

        public string NextProgramStopTime
        {
            get
            {
                return nextProgramStopTime;
            }
            set
            {
                nextProgramStopTime = value;
                RaisePropertyChanged("NextProgramStopTime");
            }
        }

        public string ProgramUrl
        {
            get
            {
                return programUrl;
            }
            set
            {
                programUrl = value;
                RaisePropertyChanged("ProgramUrl");
            }
        }

        public string NextProgramUrl
        {
            get
            {
                return nextProgramUrl;
            }
            set
            {
                nextProgramUrl = value;
                RaisePropertyChanged("NextProgramUrl");
            }
        }

        public Uri PreferredFormattedStreamingUrl
        {
            get
            {
                Url url =
                    Channel.StreamingUrl.Urls.Where(
                        p => p.Type.Equals("aac", StringComparison.InvariantCultureIgnoreCase)
                             && p.Protocol.Equals("http", StringComparison.InvariantCultureIgnoreCase)
                             && p.Quality.Equals("low", StringComparison.InvariantCultureIgnoreCase)
                             && p.PlayList != null
                             && p.PlayList.Equals("m3u", StringComparison.InvariantCultureIgnoreCase)).Single();

                return new Uri(url.Link.Replace(".m3u", ".mp3"));
            }
        }

        public Microsoft.Phone.BackgroundAudio.AudioTrack ToAudioTrack()
        {
            if(string.IsNullOrWhiteSpace(Channel.Logo))
                return new Microsoft.Phone.BackgroundAudio.AudioTrack(PreferredFormattedStreamingUrl, "Sveriges Radio", Channel.Name, "", null, "", Microsoft.Phone.BackgroundAudio.EnabledPlayerControls.None);

            return new Microsoft.Phone.BackgroundAudio.AudioTrack(PreferredFormattedStreamingUrl, "Sveriges Radio", Channel.Name, "", new Uri(Channel.Logo), "", Microsoft.Phone.BackgroundAudio.EnabledPlayerControls.None);

        }

    }
}
